package com.siecom.module;

import android.os.Bundle;

import com.siecom.hostservice.HKBaseApplication;
import com.siecom.tool.Timer;

/**
 * Created by zhq on 2017/1/17.
 */

public class FingerPrintRunnable extends ModuleRunnable {

    private final static String TAG = "FingerPrintRunnable";

    private static Action action;




    private static TesoFingerprintModule tesoFingerPrintModule = TesoFingerprintModule.getInstance();

    public static enum Action {
        READ_VER,
        READ_FEATURE,
        GET_PIC
    }

    public FingerPrintRunnable(Action action, final DeviceCallback callback, final int timeOutMs, final boolean isOnMain) {
        this.callback = callback;
        this.timeOutMs = timeOutMs;
        this.isOnMain = isOnMain;
        this.action = action;
//        InitFinger();

    }

    @Override
    public void stopRun() {
        this.runFlag = false;
        tesoFingerPrintModule.setStopRun(true);
    }

    @Override
    public void run() {
        synchronized (this) {
            Timer tm = null;
            if (timeOutMs > 0) {
                tm = new Timer(timeOutMs);
                tm.start();
            }

//            tesoFingerPrintModule.PowerOn();
//            tesoFingerPrintModule.init();
//            SystemClock.sleep(1200);
            deliverResult(callback, DeviceInterface.Result.START, null, isOnMain);
            try {
//                while (runFlag) {
//                    byte[] data = new byte[60000];
//                    int[] len = new int[1];
//                    if (timeOutMs > 0) {
//                        if (tm.timeOut()) {
//                            Log.e(TAG, "tm.timeOut!!");
//                            Bundle bundle = new Bundle();
//                            bundle.putInt("code", ErrorDefine.FINGERPRINT_FREATURE_TIMEOUT);
//                            bundle.putString("message", ErrorDefine.getErrorDescribe(ErrorDefine.FINGERPRINT_FREATURE_TIMEOUT));
//                            deliverResult(callback, DeviceInterface.Result.ERROR, bundle, isOnMain);
//                            break;
//                        }
//                    }
                    if (action == Action.READ_FEATURE) {
//                        tesoFingerPrintModule.flush();
//                        tesoFingerPrintModule.lightOn();
//                        int  res = tesoFingerPrintModule.getFeature(data, len);
//                        Log.e("res",res+"---");
//                        if ( res == 0) {
//                            Log.e(TAG, ByteTool.bytearrayToHexString(data, len[0]));
//                            Bundle bundle = new Bundle();
//                            bundle.putString("data", ByteTool.bytearrayToHexString(data, len[0]));
//                            deliverResult(callback, DeviceInterface.Result.SUCCEED, bundle, isOnMain);
//                            tesoFingerPrintModule.lightOff();
//                            return;
//                        } else {
//                            SystemClock.sleep(100);
//                            tesoFingerPrintModule.lightOff();
//                            continue;
//                        }

//                        mTcFingerClientApi.DLL_tcGetFeature(30000, new Callback() {
//
//                            @Override
//                            public void onSuccess(FingerResult result) {
//                                byte[] template = result.getTemplate();
//
////				byte[] gbBytes = FpImage.tcFeaToGB(template);
////
////				byte[] feaBytes = FpImage.gbToTcFea(gbBytes);
//
//                                mTemplate = template;
////				String feaStr = Base64.encodeToString(template, Base64.NO_WRAP);
//                                // mHintTv.setText("鑾峰彇鐗瑰緛鎴愬姛 gbBytes:" + gbBytes.length +
//                                // " feaBytes:" + feaBytes.length);
//
//                                Logs.e(TAG, "特征" + new String(template));
//                            }
//
//                            @Override
//                            public void onError(int errorCode) {
//                                Bundle bundle = new Bundle();
//                                bundle.putInt("code", ErrorDefine.FINGERPRINT_FREATURE_TIMEOUT);
//                                bundle.putString("message", ErrorDefine.getErrorDescribe(ErrorDefine.FINGERPRINT_FREATURE_TIMEOUT));
//                                deliverResult(callback, DeviceInterface.Result.ERROR, bundle, isOnMain);
//                            }
//                        });

                    }


                    if(action==Action.GET_PIC){
//                        SystemClock.sleep(500);
//                                tesoFingerPrintModule.flush();
//                                tesoFingerPrintModule.lightOn();
//                                int  res = tesoFingerPrintModule.checkFingerPicture(data,len);
//                                Log.e("check_res",res+"---");
//                                if ( res == 0) {
//                                    byte[] fingerData = new byte[1024];
//                                    int[] len1 = new int[1];
//                                    res = tesoFingerPrintModule.getFingerPicture(data,len);
//                                    tesoFingerPrintModule.getFeature(fingerData, len1);
//                                    Log.e("get_res",res+"---");
//                                    if(res==0) {
//                                        //  Log.e(TAG, ByteTool.bytearrayToHexString(data, len[0]));
//                                        Log.e(TAG,"len=="+len[0]);
//                                        byte[] buff = new byte[len[0]-28];
//                                        Bundle bundle = new Bundle();
//                                        System.arraycopy(data,28,buff,0,len[0]-28);
//                                bundle.putString("data", ByteTool.bytearrayToHexString(fingerData, len1[0]));
//                                bundle.putInt("zip",1);
//                                bundle.putByteArray("imgBuff",buff);
//                                deliverResult(callback, DeviceInterface.Result.SUCCEED, bundle, isOnMain);
//                                tesoFingerPrintModule.lightOff();
//                                return;
//                            }
//
//                        }

                    }


                    if (action == Action.READ_VER) {
//                        if (tesoFingerPrintModule.readVersion(data, len) == 0) {
//                            Log.e("ver", ByteTool.bytearrayToHexString(data, len[0]));
//                            Bundle bundle = new Bundle();
//                            bundle.putString("data", ByteTool.bytearrayToHexString(data, len[0]));
//                            deliverResult(callback, DeviceInterface.Result.SUCCEED, bundle, isOnMain);
//                            return;
//                        }
//                    }
//                }
                    }
            } catch (Exception e) {
                e.printStackTrace();
                HKBaseApplication.saveCatchInfo2File(e);
            } finally {
                deliverResult(callback, DeviceInterface.Result.COMPLETED, new Bundle(), isOnMain);
                tesoFingerPrintModule.PowerOff();
                tesoFingerPrintModule.lightOff();
                return;
            }
        }
    }
}

